import { GameServer } from "../Game/GameServer";

import ws = require("nodejs-websocket");

export enum SocketType {
    setName = 1,        //填写昵称
    reqHost = 2,        //登录          
    game = 3,           //申请主持游戏  成功或者失败  isSuccess ： 1 成功 0 失败
    wirteResule = 4,    //填写谜底
    exitGame = 5,       //退出游戏
    joinRoom = 6,       //加入房间
    sendResule = 7,     //发送答案
    errorLog = 8,       //提示
    GameEnd = 9,        //结算
    ReturnSelfRoom = 10, //返回自己的房间
    readyGame = 11      //准备游戏
}

export enum GameState {
    start = 1,      //开始
    end = 2,        //结束    
    ready = 3       //准备      
}


export class ScoketServer {

    public m_Server: any;
    public static Instance: ScoketServer = null;
    public createServer() {
        ScoketServer.Instance = this;
        this.m_Server = ws.createServer((conn) => {
            console.log("建立连接");
            conn.on('text', this.onReceive.bind(this, conn));
            conn.on('error', () => { console.log("err") });
            conn.on("close", (code, reason) => { console.log("断开连接") });
            conn.on('connect', function (code) { console.log('开启连接', code) });
        }).listen(3601);
    }

    /**接收客户端数据 */
    onReceive(conn, msg) {

        console.log('接收 ' + msg);
        msg = JSON.parse(msg);

        switch (msg.socketType) {
            case SocketType.setName:
                GameServer.GetInst().setgameinfo(conn, msg);
                break;
            case SocketType.joinRoom:
                GameServer.GetInst().joinOtherRoom(conn, msg);
                break;
            case SocketType.reqHost:
                GameServer.GetInst().ReqHostMethod(conn, msg);
                break;
            case SocketType.wirteResule:
                GameServer.GetInst().setResule(conn, msg);
                break;
            case SocketType.game:
                GameServer.GetInst().onDrow(conn, msg);
                break;
            case SocketType.sendResule:
                GameServer.GetInst().onUserResuleReq(conn, msg);
                break;
            case SocketType.ReturnSelfRoom:
                GameServer.GetInst().ReturnSelfRoom(conn, msg);
                break;
            case SocketType.readyGame:
                GameServer.GetInst().ReadGame(conn, msg);
                break;
            default:
                break;
        }
    }

    /**
     * 
     * @param conn socket链接
     * @param data 数据
     * @param socketType 协议类型
     */
    onSend(conn, data, socketType) {
        let objData = { _data: data, socketType: socketType }
        let msg = JSON.stringify(objData);
        conn.sendText(msg);
        console.log('向玩家发送 :' + msg);
    }

    OnError(conn, errStr: string) {
        let msg = JSON.stringify({ error: errStr, socketType: SocketType.errorLog });
        conn.sendText(msg);
        console.log('错误发送 :' + msg);
    }


    broadcast(msg: string) {
        this.m_Server.connections.forEach(function (conn) {
            console.log('发送 ' + msg);
            conn.sendText(msg)
        })
    }

    CloseSrever() {
        this.m_Server.close();
    }

    NewServer(port: number, host: string, callback?: Function) {
        this.m_Server.listen(port, [host], [callback])
    }

}
